package gr.handmademarket.ui.pages;

import gr.handmademarket.po.implementations.Buyer;
import gr.handmademarket.po.implementations.Seller;
import gr.handmademarket.po.interfaces.User;
import gr.handmademarket.ui.WebHandMadeSession;
import gr.handmademarket.ui.forms.LoginForm;
import gr.handmademarket.ui.forms.RegisterForm;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.IAjaxIndicatorAware;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.bootstrap.Bootstrap;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.resource.ContextRelativeResource;

/**
 * template page for most of the pages.
 */
public class CommonTemplatePage 
extends WebPage
implements IAjaxIndicatorAware{

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * the welcome message.
	 */
	private Label welcomeMessage;
	
	private LoginForm loginForm = new LoginForm("loginForm", new CompoundPropertyModel<User>(new Buyer()));
	
	private RegisterForm registerForm = new RegisterForm("registerForm", new CompoundPropertyModel<Buyer>(new Buyer()));
		
	/**
	 * the feedbackPanel
	 */
	@SuppressWarnings("nls")
	private FeedbackPanel feedbackPanel = new FeedbackPanel("feedbackPanel"); 
	
	/**
	 * sign out link.
	 */
	@SuppressWarnings("nls")
	private AjaxLink<String> signOutLink = new AjaxLink<String>("signOutLink") {

		/**
		 * serialVersionUID
		 */
		private static final long serialVersionUID = 1L;

		@Override
		public void onClick(AjaxRequestTarget target) {
			WebHandMadeSession session = (WebHandMadeSession) WebHandMadeSession.get();
			session.setUser(null);
			signOutLink.setVisible(false);
			sellerAdminLink.setVisible(false);
			welcomeMessage.setVisible(false);
			Label localMessage = new Label("welcomeMsg", "");
			localMessage.setVisible(false);
			this.getPage().get("welcomeMsg").replaceWith(localMessage);
			target.add(this.getPage());
		}
	};
	
	/**
	 * home link.
	 */
	@SuppressWarnings("nls")
	private AjaxLink<String> homeLink = new AjaxLink<String>("homeLink") {

		/**
		 * serialVersionUID
		 */
		private static final long serialVersionUID = 1L;

		@Override
		public void onClick(AjaxRequestTarget target) {
			setResponsePage(HomePage.class);
		}
	};
	
	/**
	 * sellerAdminLink.
	 */
	@SuppressWarnings("nls")
	private AjaxLink<String> sellerAdminLink = new AjaxLink<String>("sellerAdminLink") {

		/**
		 * serialVersionUID
		 */
		private static final long serialVersionUID = 1L;

		@Override
		public void onClick(AjaxRequestTarget target) {
			setResponsePage(SellerAdminPage.class);
		}
	};
	
	/**
	 * my basket link.
	 */
	@SuppressWarnings("nls")
	private AjaxLink<String> myBasketLink = new AjaxLink<String>("myBasketLink") {

		/**
		 * serialVersionUID
		 */
		private static final long serialVersionUID = 1L;

		@Override
		public void onClick(AjaxRequestTarget target) {
			setResponsePage(MyBasketPage.class);
		}
	};
		
	/**
	 * Creates a new CommonTemplatePage object. 
	 *
	 */
	@SuppressWarnings("nls")
	public CommonTemplatePage(){
		super();
		ContextRelativeResource resource = new ContextRelativeResource("/img/rsz_babushka-03_v200.jpg");
		add(new Image("logo", resource));
		this.setOutputMarkupId(true);
		feedbackPanel.setOutputMarkupId(true);
		add(feedbackPanel,myBasketLink,signOutLink,sellerAdminLink,homeLink);
		WebHandMadeSession session = (WebHandMadeSession) WebHandMadeSession.get();
		User user = session.getUser();
		welcomeMessage = user != null ? 
				new Label("welcomeMsg", "welcome " + user.getFirstName() + " " + user.getLastName()):
					new Label("welcomeMsg", " ");
		welcomeMessage.setVisible(user != null);
		signOutLink.setVisible(user != null);
		sellerAdminLink.setVisible(user != null && user instanceof Seller);
		add(welcomeMessage, loginForm, registerForm);
		
		Model<String> classModel = new Model<String>("active");
		homeLink.getParent().add(AttributeModifier.replace("class", classModel));
	}
	
	public String getAjaxIndicatorMarkupId() {
		return "veil"; //$NON-NLS-1$
	}
	
	@Override
	public void renderHead(IHeaderResponse response) {
		super.renderHead(response);
		Bootstrap.renderHeadResponsive(response);
	}
	
}
